package com.xinxing.city.recovery.utils;

import static org.greenrobot.eventbus.EventBus.TAG;

import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.DialogFragment;

import com.xinxing.city.recovery.R;


public class MyDialog extends DialogFragment {

    // 在DialogFragment中添加成员变量和setter方法
    private MyDialogListener mListener;
    private static String ARG_TITLE = "arg_title";
    private static String ARG_CONTENT = "";
    private static String btn1Str = null;
    private static String btn2Str = null;
    private static String edStr = null;
    private EditText inputEditText;
    private View dialogView;
    private static MyDialog fragment;
    private static MyDialog fragment2;

    private AlertDialog.Builder builder;
    private static int time = 1000;
    public static MyDialog newInstance(String title, String content,String btn1,String btn2) {
        fragment = new MyDialog();
        Bundle args = new Bundle();

        btn1Str = btn1;
        btn2Str = btn2;
        ARG_TITLE = title;
        ARG_CONTENT = content;
        edStr = null;

        fragment.setArguments(args);
        return fragment;
    }
    public static MyDialog newInstance2(String title, String content,String btn1,String btn2,boolean b) {
        fragment2 = new MyDialog();
        Bundle args = new Bundle();

        btn1Str = btn1;
        btn2Str = btn2;
        ARG_TITLE = title;
        ARG_CONTENT = content;
        edStr = null;
        if(b){

            countDownTimer2.start();
        }
        fragment2.setArguments(args);
        return fragment2;
    }
    public static MyDialog newInstanceCon(String title, String content,String btn1,String btn2,String ed) {
        fragment = new MyDialog();
        Bundle args = new Bundle();

        btn1Str = btn1;
        btn2Str = btn2;
        ARG_TITLE = title;
        ARG_CONTENT = content;
        edStr = ed;

        fragment.setArguments(args);
        return fragment;

    }
    public static MyDialog newInstanceCon2(String title, String content,String btn1,String btn2,String ed,boolean b) {
        fragment2 = new MyDialog();
        Bundle args = new Bundle();

        btn1Str = btn1;
        btn2Str = btn2;
        ARG_TITLE = title;
        ARG_CONTENT = content;
        edStr = ed;
        if(b){

            countDownTimer2.start();
        }
        fragment2.setArguments(args);
        return fragment2;

    }
    public void setInputEditText(EditText inputEditText) {
        this.inputEditText = inputEditText;
    }

    public String getInputEditText() {
        EditText view = dialogView.findViewById(R.id.inputEditText);
        return view.getText().toString();
    }


    public void setMyDialogListener(MyDialogListener listener) {
        mListener = listener;
    }
    public interface MyDialogListener {
        void onPositiveClick();
        void onNegativeClick();
    }

    public void setMe(String s) {
        builder.setMessage(s);
        builder.create();
    }
    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        // 创建并配置对话框
        builder = new AlertDialog.Builder(getActivity());
        //builder.setTitle(ARG_TITLE).setMessage(ARG_CONTENT);

        LayoutInflater inflater = LayoutInflater.from(getActivity());
        dialogView = inflater.inflate(R.layout.custom_dialog, null);
        builder.setView(dialogView);

        TextView dialog_title = dialogView.findViewById(R.id.dialog_title);
        dialog_title.setText(ARG_TITLE);

        Button button1 = dialogView.findViewById(R.id.dialog_button_ok);
        if (btn1Str != null && !btn1Str.equals("")) {
            button1.setText(btn1Str);
            button1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mListener.onPositiveClick();
                }
            });
        } else {
            button1.setVisibility(View.GONE);
        }

        Button button2 = dialogView.findViewById(R.id.dialog_button_cancel);
        if (btn2Str != null && !btn2Str.equals("")) {
            button2.setText(btn2Str);
            button2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mListener.onNegativeClick();
                }
            });
        } else {
            button2.setVisibility(View.GONE);
        }

        EditText inputE = dialogView.findViewById(R.id.inputEditText);
        TextView dialog_message = dialogView.findViewById(R.id.dialog_message);

        if (edStr != null) {
            inputE.setVisibility(View.VISIBLE);
            dialog_message.setVisibility(View.GONE);

        } else {
            inputE.setVisibility(View.GONE);
            dialog_message.setVisibility(View.VISIBLE);

            dialog_message.setText(ARG_CONTENT);
        }

        return builder.create();
    }
    static CountDownTimer countDownTimer2 = new CountDownTimer(60000, 1000) {

        @Override
        public void onTick(long l) {

        }

        @Override
        public void onFinish() {
            fragment2.dismiss();
        }
    };
}
